package com.kyzh.core.pager.weal.recover

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.util.setHeight
import com.gushenge.atools.util.setWidth
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.R
import com.kyzh.core.adapters.decoration.SpacesItemDecoration
import com.kyzh.core.current.RecoverBean
import com.kyzh.core.current.WealImpl1.WealImpl1
import com.kyzh.core.databinding.ItemRecoverBinding
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.dip
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.activity_answer.*
import kotlinx.android.synthetic.main.activity_coupon.*
import kotlinx.android.synthetic.main.activity_myorder.*
import kotlinx.android.synthetic.main.activity_recover_record.*
import kotlinx.android.synthetic.main.activity_recover_record.close
import org.jetbrains.anko.textColorResource

class RecoverRecordActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Activity) {
            val starter = Intent(context, RecoverRecordActivity::class.java)
            context.startActivityForResult(starter,10011)
        }
    }
   private var  i : Int = 1
   private var adapter: RecoverAdapter = RecoverAdapter()
    private  var recoverBean :MutableList<RecoverBean.Data>?  = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recover_record)
        initView()
        initData()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        initData()
    }
    fun initView(){
        close.setOnClickListener {
            finish()
        }
        rev_record.layoutManager = LinearLayoutManager(this,RecyclerView.VERTICAL,false)
        rev_record.adapter = adapter
        val inflate = LayoutInflater.from(this@RecoverRecordActivity).inflate(R.layout.empty, null)
        adapter.setEmptyView(inflate)
        rev_record.addItemDecoration(SpacesItemDecoration(8f))
        adapter.loadMoreModule.setOnLoadMoreListener {
            i++
            WealImpl1.recoverOrder(i){
                if(this?.data == null){
                    adapter.loadMoreModule.loadMoreEnd( gone = true)
                }
                this?.let {
                    recoverBean?.addAll(it.data)
                    adapter.notifyDataSetChanged()
                    adapter.loadMoreModule.loadMoreComplete()
                    if( i == this.max_p){
                        adapter.loadMoreModule.loadMoreEnd( false)
                    }
                }
            }
        }
        smart.setEnableLoadMore(false)
        smart.setEnableAutoLoadMore(false)
        smart.setOnRefreshListener {
            i=1
            WealImpl1.recoverOrder(i){
                smart.finishRefresh()
                recoverBean  = this?.data
                adapter.setNewInstance(recoverBean)
                adapter.notifyDataSetChanged()
                adapter.loadMoreModule.loadMoreComplete()
                if( i == this?.max_p){
                    adapter.loadMoreModule.loadMoreEnd( false)
                }
            }
        }
    }
    fun initData(){
        i = 1
      WealImpl1.recoverOrder(i){
          recoverBean  = this?.data
          adapter.setNewInstance(recoverBean)
          adapter.loadMoreModule.isEnableLoadMore = i <= this?.max_p!!
          adapter.loadMoreModule.loadMoreComplete()
          if( i == this.max_p){
              adapter.loadMoreModule.loadMoreEnd( false)
          }
          adapter.notifyDataSetChanged()
      }
    }
    inner class RecoverAdapter:BaseQuickAdapter< RecoverBean.Data, BaseDataBindingHolder<ItemRecoverBinding>>(R.layout
        .item_recover,recoverBean),LoadMoreModule{
        override fun convert(holder: BaseDataBindingHolder<ItemRecoverBinding>, item: RecoverBean.Data) {
            item.huishoujia = "回收价："
            holder.dataBinding?.apply {
                data = item
                imgAccount.setHeight( dip(53))
                imgAccount.setWidth(dip(58))
                when(item.status){
                    //0 审核中，1回收成功，2拒绝回收，3已撤销
                    0 ->{
                        imgAccount.setImageResource(R.drawable.icon_record_doing)
                        tvState.text = "撤销"
                        tvState.textColorResource = R.color.a92B4FF
                        tvState.setVisibility(true)
                        tvTimeTip.setVisibility(false)
                        tvTime.setVisibility(false)
                    }
                    1 ->{
                        imgAccount.setImageResource(R.drawable.icon_record_success)
                        tvState.setVisibility(false)
                    }
                    2 ->{
                        imgAccount.setImageResource(R.drawable.icon_record_error)
                        if(item.note.isEmpty()){
                            tvState.setVisibility(false)
                        }else{
                            tvState.setVisibility(true)
                            tvState.text = "查看"
                        }
                        tvState.textColorResource = R.color.FB8976
                    }
                    else ->{
                        imgAccount.setImageResource(R.drawable.icon_record_chexiao)
                        tvState.text = "查看"
                        tvState.textColorResource = R.color.FB8976
                    }
                }
                if(item.note .isEmpty()&&item.status!=0){
                    tvState.setVisibility(false)
                }
                tvState.setOnClickListener {
                    when(item.status){
                        0 ->{
                            CheXiaoActivity.start(context as Activity, item.id)
                        }
                        else ->
                        //弹窗
                        alert("查看原因",item.note,"关闭",null,{}){
                        }
                    }
                }
            }
        }
    }
}